//
//  BGHttpTool.m
//  bigMall
//
//  Created by LTOVE on 16/7/13.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGHttpTool.h"
#import "AFNetworking.h"
#import "BGBasicResult.h"
#import "MJExtension.h"
#import "BGParam.h"
#import "MBProgressHUD+MJ.h"
#import "MBProgressHUD.h"
#import "BGSessionModel.h"

@implementation BGHttpTool

+ (void)GET:(NSString *)URLstring paramenters:(id)paramenters progress:(void (^)(NSProgress *))downProgress success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
//    [MBProgressHUD hideHUD];
//    [MBProgressHUD showMessage:nil];
    BGParam *param = [BGParam objectWithKeyValues:paramenters];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:URLstring parameters:paramenters progress:^(NSProgress * _Nonnull downloadProgress) {
        if (downProgress) {
            downProgress(downloadProgress);
        }
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        [MBProgressHUD hideHUD];
//        for (MBProgressHUD *view in BGKeyWindow.subviews) {
//            if ([view isKindOfClass:[MBProgressHUD class]]) {
//                [view removeFromSuperview];
//            }
//            GWLog(@"%@",[view class]);
//        }
        [BGHttpTool statusNotifyWithresponseObject:responseObject Action:param.action];
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        [MBProgressHUD hideHUD];
        if (failure) {
            failure(error);
        }
    }];

}

+ (void)POST:(NSString *)URLstring paramenters:(id)paramenters progress:(void (^)(NSProgress *))downProgress success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
//    [MBProgressHUD hideHUD];
//    [MBProgressHUD showMessage:nil toView:BGKeyWindow];
//    for (MBProgressHUD *view in BGKeyWindow.subviews) {
//        GWLog(@"%@    ssss",[view class]);
//        if ([view isKindOfClass:[MBProgressHUD class]]) {
//            [view removeFromSuperview];
//        }
//        
//    }
    BGParam *param = [BGParam objectWithKeyValues:paramenters];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:URLstring parameters:paramenters progress:^(NSProgress * _Nonnull uploadProgress) {
        if (downProgress) {
            downProgress(uploadProgress);
        }
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        [MBProgressHUD hideHUD];
//        [MBProgressHUD hideHUD];
//        for (MBProgressHUD *view in BGKeyWindow.subviews) {
//            GWLog(@"%@    ssss",[view class]);
//            if ([view isKindOfClass:[MBProgressHUD class]]) {
//                [view removeFromSuperview];
//            }
//            
//        }
        
        if ([param.action isEqualToString:@"login"]) {
            NSURLResponse *fields = task.response;
//            NSLog(@"---->%@",fields.JSONString);
            BGSessionModel *sessionModel = [BGSessionModel objectWithKeyValues:fields.JSONString];
            [[NSUserDefaults standardUserDefaults] setObject:sessionModel.SetCookie forKey:sessionId];
//            NSString *se = [[NSUserDefaults standardUserDefaults] objectForKey:sessionId];
//            NSLog(@" qu chu lai de %@",se);
        }
       
        
        [BGHttpTool statusNotifyWithresponseObject:responseObject Action:param.action];
        NSLog(@"param.action === %@",param.action);
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        [MBProgressHUD hideHUD];
//        for (MBProgressHUD *view in BGKeyWindow.subviews) {
//            if ([view isKindOfClass:[MBProgressHUD class]]) {
//                [view removeFromSuperview];
//            }
//            GWLog(@"%@",[view class]);
//        }
        NSLog(@"%ld",(long)error.code);
        if (error.code == -1009) {
            if (success) {
                success(nil);
            }
        }
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"loadError" object:nil userInfo:nil];
        if (failure) {
            failure(error);
        }
    }];
}

+ (void)POSTImage:(NSString *)URLstring paramenters:(id)paramenters progress:(void (^)(NSProgress *))downProgress success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    
//    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
//    [manager POST:URLstring parameters:paramenters progress:^(NSProgress * _Nonnull uploadProgress) {
//        if (downProgress) {
//            downProgress(uploadProgress);
//        }
//    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
////        [BGHttpTool statusNotifyWithresponseObject:responseObject];
//        if (success) {
//            success(responseObject);
//        }
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
////        NSLog(@"%ld",(long)error.code);
////        if (error.code == -1009) {
////            if (success) {
////                success(nil);
////            }
////        }
//        
////        [[NSNotificationCenter defaultCenter] postNotificationName:@"loadError" object:nil userInfo:nil];
//        if (failure) {
//            failure(error);
//        }
//    }];
//
    
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:URLstring parameters:paramenters progress:^(NSProgress * _Nonnull downloadProgress) {
        if (downProgress) {
            downProgress(downloadProgress);
        }
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        [BGHttpTool statusNotifyWithresponseObject:responseObject];
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];

}

+ (void)statusNotifyWithresponseObject:(id)responseObject Action:(NSString *)action
{
    BGBasicResult *model = [BGBasicResult objectWithKeyValues:responseObject];
    NSDictionary *dic;
   
    if (action == nil) {
        action = @"";
    }
    
        dic = @{
                @"status":model.status,
                @"className":action
                };
  
    [[NSNotificationCenter defaultCenter] postNotificationName:@"dataStatus" object:nil userInfo:dic];
}

@end
